﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class Android_Record : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["username"] != null)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            SqlCommand cmd;
            SqlDataReader reader;



            try
            {
                bool isExist = false;

                conn.Open();
                cmd= new SqlCommand("SELECT * FROM [Schedule] WHERE [UserId]=@UserId AND [Date]=@date",conn);
                cmd.Parameters.AddWithValue("@UserId", Request.QueryString["username"]);
                cmd.Parameters.AddWithValue("@Date", Request.QueryString["Date"]);
                reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    isExist = true;
                    if(isExist && Request.QueryString["Type"] != "update")
                    Response.Write("exist data");
                }
                reader.Close();

                if (!isExist)
                {
                    cmd = new SqlCommand("INSERT INTO [Schedule] ([CalorieAbsorbed], [TimeExercise], [Weight], [WaterAbsorbed], [Date], [UserId]) VALUES (@CalorieAbsorbed, @TimeExercise, @Weight, @WaterAbsorbed, @Date, @UserId)", conn);
                    cmd.Parameters.AddWithValue("@CalorieAbsorbed", Request.QueryString["CalorieAbsorbed"]);
                    cmd.Parameters.AddWithValue("@TimeExercise", Request.QueryString["TimeExercise"]);
                    cmd.Parameters.AddWithValue("@Weight", Request.QueryString["Weight"]);
                    cmd.Parameters.AddWithValue("@WaterAbsorbed", Request.QueryString["WaterAbsorbed"]);
                    cmd.Parameters.AddWithValue("@Date", Request.QueryString["Date"]);
                    cmd.Parameters.AddWithValue("@UserId", Request.QueryString["username"]);
                    cmd.ExecuteNonQuery();
                    Response.Write("success");
                }

                if (isExist && Request.QueryString["Type"] == "update")
                {
                    cmd = new SqlCommand("UPDATE [Schedule] SET [CalorieAbsorbed] = @CalorieAbsorbed, [TimeExercise] = @TimeExercise, [Weight] = @Weight, [WaterAbsorbed] = @WaterAbsorbed WHERE [UserId] = @UserId AND [Date]=@Date", conn);
                    cmd.Parameters.AddWithValue("@CalorieAbsorbed", Request.QueryString["CalorieAbsorbed"]);
                    cmd.Parameters.AddWithValue("@TimeExercise", Request.QueryString["TimeExercise"]);
                    cmd.Parameters.AddWithValue("@Weight", Request.QueryString["Weight"]);
                    cmd.Parameters.AddWithValue("@WaterAbsorbed", Request.QueryString["WaterAbsorbed"]);
                    cmd.Parameters.AddWithValue("@Date", Request.QueryString["Date"]);
                    cmd.Parameters.AddWithValue("@UserId", Request.QueryString["username"]);
                    cmd.ExecuteNonQuery();
                    Response.Write("update success");
                }

            }
            catch(Exception ex)
            {
                Response.Write("error: "+ ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}